﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Employee_CreateInterview : System.Web.UI.Page
{
    String UserName = "";
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            UserName = Session["User"].ToString();
            if (CheckDateTime(txtDateInterview.Text, txtHours.Text, txtMinutes.Text))
            {
                ddltitlevacancy.Enabled = true;
            }
            else
            {
                ddltitlevacancy.Enabled = false;
            }
            if (ddltitlevacancy.SelectedIndex == 0)
            {
                txtDateInterview.Focus();
                ddlApplicant.Enabled = false;
                ddlInterviewer.Enabled = false;
            }
            else
            {
                ddlApplicant.Enabled = true;
                ddlInterviewer.Enabled = true;
            }
            txtEmployeeCreate.Text = EM.ReturnNameEmployee(UserName);
            txtDateInterview_CalendarExtender.StartDate = DateTime.Today;
            AddItemsVacancy();
            btnCreate.Enabled = false;
        }
        catch (Exception ex)
        {
        }
    }
    private void AddItemsVacancy()
    {
        try
        {
            if (ddltitlevacancy.Items.Count <= 1)
            {
                SqlDataReader dr = EM.ReturnVacancyWithUserName(UserName);
                ddltitlevacancy.DataSource = dr;
                ddltitlevacancy.DataTextField = "Title";
                ddltitlevacancy.DataValueField = "VacancyID";
                ddltitlevacancy.DataBind();
                EM.connClose();
                ddltitlevacancy.Items.Insert(0, "Please choise title vacancy");
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void AddItemsApplicant()
    {
        try
        {
            SqlDataReader dr = EM.ReturnApplicantWithVacancy(ddltitlevacancy.SelectedValue);
            ddlApplicant.DataSource = dr;
            ddlApplicant.DataTextField = "FullName";
            ddlApplicant.DataValueField = "ApplicantID";
            ddlApplicant.DataBind();
            EM.connClose();
            ddlApplicant.Items.Insert(0, "Please choise Applicant");
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void AddItemsInterviewer()
    {
        try
        {
            SqlDataReader dr = EM.ReturnInterviewer();
            ddlInterviewer.DataSource = dr;
            ddlInterviewer.DataTextField = "FullName";
            ddlInterviewer.DataValueField = "InterViewerID";
            ddlInterviewer.DataBind();
            EM.connClose();
            ddlInterviewer.Items.Insert(0, "Please choise Interviewer");
            for (int i = 1; i < ddlInterviewer.Items.Count; i++)
            {
                if (EM.CheckInterviewBusy(ddlInterviewer.Items[i].Value.ToString(), txtDateInterview.Text, txtHours.Text +"h"+ txtMinutes.Text))
                {
                    ddlInterviewer.Items.RemoveAt(i);
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        if (txtDateInterview.Text.Equals(""))
        {
            lblErrorDateInterview.Text = "(*)";
            lblError.Text = "Date Interview can't be empty.";
        }

        else
        {
            lblErrorDateInterview.Text = "";
            if (txtHours.Text.Equals(""))
            {
                lblErrorTime.Text = "(*)";
                lblError.Text = "Time Details can't be empty.";
            }
            else
            {
                lblErrorTime.Text = "";
                if (ddltitlevacancy.SelectedValue.Equals("Please choise title vacancy"))
                {
                    lblErrortitlevacancy.Text = "(*)";
                    lblError.Text = "Please choise value for title vacancy.";
                }
                else
                {
                    lblErrortitlevacancy.Text = "";
                    if (ddlApplicant.SelectedValue.Equals("Please choise Applicant"))
                    {
                        lblErrorApplicant.Text = "(*)";
                        lblError.Text = "Please choise value for Applicant Name.";
                    }
                    else
                    {
                        lblErrorApplicant.Text = "";
                        if (ddlInterviewer.SelectedValue.Equals("Please choise Interviewer"))
                        {
                            lblErrorInterviewer.Text = "(*)";
                            lblError.Text = "Please choise value for Interviewer.";
                        }
                        else
                        {
                            lblErrorInterviewer.Text = "";
                            lblError.Text = "";
                            try
                            {
                                DateTime dateinterview = Convert.ToDateTime(txtDateInterview.Text.ToString());
                                string stime = txtHours.Text + "h" + txtMinutes.Text;
                                bool result = EM.InsertIntoTableInterview(ddlInterviewer.SelectedValue, ddltitlevacancy.SelectedValue, ddlApplicant.SelectedValue, dateinterview, stime);
                                if (result)
                                {
                                    Response.Write("<script type='text/javascript'>var re = confirm('Add a new interview success. \\n Do you want to continued?');if(re){window.location = 'CreateInterview.aspx';}else{window.location = 'HRViewIntervewInformation.aspx';} return false;</script>");
                                }
                                else
                                {
                                    lblError.Text = "Add interview faild.";
                                }
                            }
                            catch (Exception ex)
                            {
                                lblErrorDateInterview.Text = "(*)";
                                lblError.Text = "Check Date interview";
                                Console.Write(ex.Message);
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddltitlevacancy.ClearSelection();
        ddlApplicant.ClearSelection();
        ddlInterviewer.ClearSelection();
        txtDateInterview.Text = "";
        txtHours.Text = "Hours";
        txtMinutes.Text = "Minutes";
        lblError.Text = "";
        lblErrorApplicant.Text = "";
        lblErrorDateInterview.Text = "";
        lblErrorInterviewer.Text = "";
        lblErrorTime.Text = "";
        lblErrortitlevacancy.Text = "";
        lblSuccess.Text = "";
        ddltitlevacancy.Enabled = false;
        ddlInterviewer.Enabled = false;
        ddlApplicant.Enabled = false;
    }
    protected void ddltitlevacancy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddltitlevacancy.SelectedIndex != 0)
        {
            for (int i = 0; i < ddlApplicant.Items.Count; i++)
            {
                ddlApplicant.Items.RemoveAt(i);
            }
            for (int i = 0; i < ddlInterviewer.Items.Count; i++)
            {
                ddlInterviewer.Items.RemoveAt(i);
            }
            AddItemsApplicant();
            AddItemsInterviewer();
        }
        else
        {
            ddlApplicant.Items.Clear();
            ddlInterviewer.Items.Clear();
            ddlApplicant.Items.Insert(0, "Please choise Applicant");
            ddlInterviewer.Items.Insert(0, "Please choise Interviewer");
        }
    }
    [System.Web.Services.WebMethod]
    private static bool CheckDate(string _Date)
    {
        bool result = false;
        try
        {
            DateTime date = Convert.ToDateTime(_Date);
            if (date.Date > DateTime.Now)
            {
                result = true;
            }
        }
        catch (Exception)
        {
        }
        return result;
    }
    [System.Web.Services.WebMethod]
    public static bool CheckDateTime(string _Date, string _Hours, string _Minutes)
    {
        bool result = false;
        try
        {
            if (CheckDate(_Date))
            {
                int hours = Convert.ToInt32(_Hours);
                int minutes = Convert.ToInt32(_Minutes);
                if ((hours >= 0) && (hours < 25) && (minutes >= 0) && (minutes < 60))
                {
                    result = true;
                }
            }
        }
        catch (Exception)
        {
        }
        return result;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script language='javascript'>alert('asd');}</script>");
    }
}